package com.guxiuwei.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guxiuwei.sys.common.Result;
import com.guxiuwei.sys.entity.Aporder;
import com.guxiuwei.sys.entity.User;
import com.guxiuwei.sys.entity.Worder;
import com.guxiuwei.sys.mapper.WorderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谷秀伟
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/sys/worder")
public class WorderController {
    @Autowired
    private WorderMapper worderMapper;

    //获取前端传入的默认显示页码，页面容量数，
    // 和搜索框查询条件，对数据库中工单信息表进行分页查询
    @GetMapping("/selectAll")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search ) throws ParseException {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = sdf.parse(sdf.format(d));
        System.out.println(nowTime);

        QueryWrapper<Worder> wrapper1 = new QueryWrapper<>();
        wrapper1.lt("endday",nowTime);
        List<Worder> suuccessedList = worderMapper.selectList(wrapper1);
        for (Worder worder : suuccessedList) {
            worder.setStatus("已完成");
            worderMapper.updateById(worder);
        }

        //System.out.println(pageNum+"=="+pageSize+"=="+search);
        QueryWrapper<Worder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("oid");
        if (StrUtil.isNotBlank(search)){
            wrapper.like("oid",search);
        }
        Page<Worder> worderPage = worderMapper.selectPage(new Page<>(pageNum, pageSize),wrapper );

        return Result.success(worderPage);
    }

    //将worder对象存入数据库中，并判断该工单是否已经存在，设置新工单状态为待审批
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('客户经理')")
    public Result<?> save(@RequestBody Worder worder){

        worder.setStatus("待审批");
//        System.out.println(user);
        worderMapper.insert(worder);
            return Result.success();

    }


    //对前端传入的worder对象进行修改更新操作
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('客户经理')")
    public Result<?> update(@RequestBody Worder worder){
        worderMapper.updateById(worder);
        return Result.success();
    }

    @PutMapping("/finish")
    @PreAuthorize("hasAuthority('云平台维护室')")
    public Result<?> finish(@RequestBody Worder worder){
        worderMapper.updateById(worder);
        return Result.success();
    }

    //得到待审批工单申请数量 返回给管理者
    @GetMapping("/countStatus")
    public Result<?> countStatus(){
        QueryWrapper<Worder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","待审批");
        int count = worderMapper.selectCount(queryWrapper);
        return Result.success(count);
    }

}

